﻿var pieChart = function (json) {
    var ooSettings = {
        chart: {
            renderTo: 'pie',
            type: 'column',
            marginRight: 80,
            marginBottom: 95
        },
        title: {
            text: '婚姻调查结果图示',
            x: -20 //center
        },
        subtitle: {
            text: '来源：http://www.xxxx.com/',
            x: -20
        },
        xAxis: {
            categories: json.chart_category,
            labels: {
                rotation: -45,
                align: 'right',
                style: {
                    font: 'normal 13px Verdana, sans-serif'
                }
            }
        },
        yAxis: {
            title: {
                text: '分值情况'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        tooltip: {
            formatter: function () {
                return '<b>' + this.series.name + '</b><br/>' +
					this.x + ': ' + this.y;
            }
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'top',
            x: -10,
            y: 100,
            borderWidth: 0
        },
        series: []
    };

    var personCnt = dft_get_qry("seqno").split(',').length;
    for (var i = 0; i < personCnt; i++) {
        if (json.chart_serials[i] != undefined)
            ooSettings.series.push(
            {
                name: json.chart_serials[i].Key.split('.')[1],
                data: json.chart_serials[i].Value
            });
    }
    return new Highcharts.Chart(ooSettings);
}
$(function () {
    $.ajaxSetup({
        type: "post",
        dataType: "json",
        httpMethod: "POST",
        contentType: "application/json;charset=utf-8"
    });

    $('.title').attr('style', 'cursor:pointer').show()
    .toggle(
    function () { $(this).find('~div').slideUp('slow'); $(this).removeClass("expand").addClass("colapse"); },
    function () { $(this).find('~div').slideDown('slow'); $(this).removeClass("colapse").addClass("expand"); });
    $.ajax({
        "url": svcaddr  + "GetUserReportListWithAdmin",
        "data": '{ "seqnos": "' + dft_get_qry("seqno") + '"}',
        "success": function (json) {
            json = eval("(" + json.d + ")");

            var personCnt = dft_get_qry("seqno").split(',').length;
            for (var k = 0; k < json.a.aaData.length; k++) {
                var array = json.a.aaData[k].pop();
                if (array != undefined) {
                    for (var j = 0; j < personCnt; j++) {
                        json.a.aaData[k].push(array[j] == undefined ? "" : array[j][1]);
                    }
                }
            }
            for (var k = 0; k < json.q.aaData.length; k++) {
                var array = json.q.aaData[k].pop();
                if (array != undefined) {
                    for (var j = 0; j < personCnt; j++) {
                        json.q.aaData[k].push(array[j] == undefined ? "" : array[j][1]);
                    }
                }
            }
            for (var i = 0; i < personCnt; i++) {
                $('#tblQues>thead>tr:last').append('<td>' + json.u[i] + '</td>');
                $('#tblResult>thead>tr:last').append('<td>' + json.u[i] + '</td>');
            }
            $('#tblQues>thead>tr:first>th:last').attr("colspan", personCnt);
            $('#tblResult>thead>tr:first>th:last').attr("colspan", personCnt);

            var oTableQues = $('#tblQues').dataTable({
                "bProcessing": true,
                "bAutoWidth": true,
                "sDom": 'f<"clear">rt<"bottom">p',
                "aaData": json.q.aaData,
                "oTableTools": {
                    "sSwfPath": "media/swf/copy_csv_xls_pdf.swf",
                    "aButtons": [
				        "copy",
				        "print",
				        {
					        "sExtends":    "collection",
					        "sButtonText": 'Save <span class="caret" />',
					        "aButtons":    [ "csv", "xls", "pdf" ]
				        }
			        ]
                }
            });

            var oTableResult = $('#tblResult').dataTable({
                "bProcessing": true,
                "bAutoWidth": true,
                "sDom": 'f<"clear">rt<"bottom">',
                "bPaginate": false,
                "aaData": json.a.aaData,
                "oTableTools": {
                    "sSwfPath": "media/swf/copy_csv_xls_pdf.swf",
                    "aButtons": [ "copy", "print" ]
                },

            });

            // chart
            pieChart(json);
        }
    });
});